package com.firefighting.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.firefighting.dao.EnterpriseMapper;
import com.firefighting.entity.Enterprise;

@Service
public class EnterpriseService
{
	@Autowired
	private EnterpriseMapper enterpriseMapper;
	
	/**
	 * @author zhyan
	 * @date 2018.6.4 17:56
	 * @function 根据企业id查询企业
	 */
	public Enterprise getEneterpriseById(int id)
	{
		return enterpriseMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * @author zhyan
	 * @date 2018.6.4 17:57
	 * @function 根据id更新企业信息，将enterprise中的信息更新到数据库中
	 */
	public int updateEnterpriseInfo(int id, Enterprise enterprise)
	{
		//enterpriseMapper.deleteByPrimaryKey(id);
		//return enterpriseMapper.insertSelective(enterprise);
		return enterpriseMapper.updateByPrimaryKeySelective(enterprise);
	}
	
	public void updateEnterpriseNameById(int id, String name)
	{
		enterpriseMapper.updateNameById(name, id);
	}
	
	//张晗    添加企业
	public void addEnterprise(Enterprise e) {
		// TODO Auto-generated method stub
		enterpriseMapper.insertEnterprise(e);
	}
	
	/**
	 * 根据省市县信息内容企业信息
	 * @param province 省
	 * @param city 市
	 * @param area 县/区
	 * @return 企业信息列表
	 */
	@SuppressWarnings("rawtypes")
	public List<HashMap> queryEnterpriseByLocation(String province, String city, String area)
	{
		List<HashMap> result = new ArrayList<HashMap>();
		
		if(!area.equals(""))
		{
			//System.out.println("1 province = " + province + ", city = " + city + ", area = " + area);
			result = enterpriseMapper.selectByProvinceAndCityAndArea(province, city, area);//省市县三级查询
		}
		else
		{
			if(!city.equals(""))
			{
				//System.out.println("2 province = " + province + ", city = " + city + ", area = " + area);
				result = enterpriseMapper.selectByProvinceAndCity(province, city);//省市两级查询
			}
			else
			{
				//System.out.println("3 province = " + province + ", city = " + city + ", area = " + area);
				result = enterpriseMapper.selectByProvince(province);//省一级查询
			}
		}
		
		return result;
	}
	
	//张晗   获取所有维保企业
	public List<Enterprise> getAllEnterprise()
	{
		return enterpriseMapper.selectAllEnterprise();
	}
    //Liuxc 根据省获取企业ID列表
	public List<Integer> getEnterpriseByProvence(String province) {
		// TODO Auto-generated method stub
		return enterpriseMapper.getEnterpriseByProvence(province);
	}
	//Liuxc 根据市获取企业ID列表
	public List<Integer> getEnterpriseByCity(String city) {
		// TODO Auto-generated method stub
		return enterpriseMapper.getEnterpriseByCity(city);
	}
	
	/**
	 * @author zhyan
	 * @function 根据省查询企业信息列表
	 * @date 2018.6.10
	 */
	@SuppressWarnings("rawtypes")
	public List<HashMap> queryByProvince(String province)
	{
		return enterpriseMapper.selectByProvince(province);
	}
	
	/**
	 * @author zhyan
	 * @function 根据省和市信息查询企业信息列表
	 * @date 2018.6.10
	 */
	@SuppressWarnings("rawtypes")
	public List<HashMap> queryByProvinceAndCity(String province, String city)
	{
		return enterpriseMapper.selectByProvinceAndCity(province, city);
	}
	
	/**
	 * @author zhyan
	 * @function 根据省市区信息查询企业信息列表
	 * @date 2018.6.10
	 */
	@SuppressWarnings("rawtypes")
	public List<HashMap> queryByProvinceAndCityAndArea(String province, String city, String area)
	{
		return enterpriseMapper.selectByProvinceAndCityAndArea(province, city, area);
	}
	
	/**
	 * @author zhyan
	 * @function 返回hash形式的企业信息集合
	 * @date 2018.6.11
	 */
	@SuppressWarnings("rawtypes")
	public List<HashMap> queryAllEnterpriseHash()
	{
		return enterpriseMapper.selectAllEnterpriseHash();
	}
	
	/**
	 * @author zhyan
	 * @param 维保企业id列表
	 * @function 查询维保企业id及其名称和对应的业主名称、项目负责人名称列表
	 * @date 2018.6.11
	 */
	@SuppressWarnings("rawtypes")
	public List<HashMap> queryPEPM(List<Integer> list)
	{
		return enterpriseMapper.selectProprietorNameAndEnterpriseNameAndProjectManagerWithEnterpriseIdList(list);
	}
	
	/**
	 * @author zhyan
	 * @function 查询所有的维保企业的id列表
	 * @date 2018.6.11
	 */
	public List<Integer> queryAllEnterpriseIdList()
	{
		return enterpriseMapper.selectIdList();
	}
	
	/**
	 * @author zhyan
	 * @function 根据省和市信息查询企业id列表
	 * @date 2018.6.30
	 */
	public List<Integer> queryEnterpriseIdListByProvinceAndCity(String province, String city)
	{
		return enterpriseMapper.selectEnterpriseIdListByProvinceAndCity(province, city);
	}

	public void insertSelective(Enterprise enterprise) {
		// TODO Auto-generated method stub
		enterpriseMapper.insertSelective(enterprise);
	}
}